//
//  RootTableViewController.m
//  YinYueBoFang_Forever
//
//  Created by lanou3g on 15/11/5.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "RootTableViewController.h"
#import "TableViewCell.h"
#import "NextViewController.h"
#import "DataHandle.h"
#import "UIImageView+WebCache.h"
//下拉刷新头文件
#import "MJRefresh.h"
//下拉刷新样式头文件
#import "MJChiBaoZiHeader.h"
#import "MJDIYHeader.h"

@interface RootTableViewController ()
@property(nonatomic,strong)UIImageView *backImageView;


@end

@implementation RootTableViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"cell"];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(finishloadData) name:@"finishData" object:nil];
//    UIVisualEffectView *visualView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
    self.backImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
//    visualView.frame = [UIScreen mainScreen].bounds;
//    [self.backImageView addSubview:visualView];
    self.tableView.separatorEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    self.backImageView =[[UIImageView alloc] initWithFrame:self.view.bounds];
        self.tableView.backgroundView = self.backImageView;
    
    //下拉刷新
    self.tableView.header =[MJChiBaoZiHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    //是否进入界面就刷新
    [self.tableView.header beginRefreshing];
    
    //设置自动切换透明度（在导航栏下面自动隐藏）
    self.tableView.header.autoChangeAlpha = YES;
    
    // 上拉刷新
    self.tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 结束刷新
            [self.tableView.footer endRefreshing];
        });
    }];
    // Do any additional setup after loading the view.
}
                       // Do any additional setup after loading the view.}
                       
                       
    
-(void)loadNewData
{

    
    
    
    // 2.模拟2秒后刷新表格UI（真实开发中，可以移除这段gcd代码）
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [[DataHandle defaultDataHandle] loadData];
        [self.tableView reloadData];
        
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tableView.header endRefreshing];
    });
}
    

-(void)finishloadData
{
    [self.backImageView sd_setImageWithURL:[NSURL URLWithString:[[[DataHandle defaultDataHandle] musicModelWithindex:0] blurPicUrl]]];

    [self.tableView reloadData];
   
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    
    DataHandle *handle = [DataHandle defaultDataHandle];
    return [handle musicModelCount];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//    cell.textLabel.text = @"12";
    DataHandle *handle = [DataHandle defaultDataHandle];
    
    MusicModel *m = [handle musicModelWithindex:indexPath.row];
    [cell.imv sd_setImageWithURL:[NSURL URLWithString:m.picUrl]];
    [cell bindModel:m];
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NextViewController *nextVC = [[NextViewController alloc] init];
    
    nextVC.index = indexPath.row;
    
    [self presentViewController:nextVC animated:YES completion:nil];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
